

// 文章列表的控制器
food.controller('ArticleListController',function($scope,$http,page,$rootScope){

	 // 声明一个全局变量
    $rootScope.art_id = 0;

     $http({
      method:'get',
      url:'../Api/admin.php?action=article_count'
     }).then(function(res){
       $scope.totalItems=res.data.c;
     });
    $scope.currentPage = 1;
    $scope.bigTotalItems = 10;
    $scope.maxSize = 5;
    var url = "../Api/admin.php?action=article_list";
    // 默认第一页的页码  数据默认第一页显示
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
        $scope.article_list = res;  


    });
    // 页码改变时 数据根据页码改变而改变
    $scope.pageChanged = function(){
        page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(res){
            $scope.article_list = res;
        });
    };


  // 该方法接收到 点击删除名令传过来的id
    $scope.deleteFood = function(food_id){
         // 全局变量  意味着真个页面都可以使用这个变量
        $rootScope.art_id = art_id;
    }

});

// 文章列表添加的控制器

food.controller('ArticleAddConroller',function($scope,$http,Upload,$location){

   // 文章有分类 先查出分类
   
   $http({
   	  method:'get',
   	  url:'../Api/admin.php?action=article_cate'
   }).then(function(res){
       $scope.article_list=res.data;
   });

   // 上传图片的插件调用
    $scope.uploadImg = function(){
        if($scope.food.art_img){
            console.log($scope.food.art_img);
            Upload.upload({
                url:"../Api/admin.php?action=article_add",
                file:$scope.food.art_img
            }).success(function(result){
                $scope.food.art_img = "uploads/"+result.art_img;
                 
                console.log($scope.food.art_img);

            });
        }
    };
   // 表单提交自己封装的方法
   
   $scope.saveForm=function(){
      
       $http({

           method:'post', //是一个数组
           url:'../Api/admin.php?action=article_add',
           data:$scope.food,

           // 设置头信息
           headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            // 数据转换  转换成一个数组
           transformRequest:function(obj){
                var str = [];
                for(var s in obj){
                    
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }
                return str.join("&");
            }
       }).then(function(res){

           if(res.data){
           	    
                 // 返回文章列表 如果添加成功
                 $location.path('article_list');
           }else{
                alert("添加失败");
                $location.path('article_list');

           }    
       });
   }
});


food.controller('ArticleAddConroller',function($scope,$http,Upload,$location){

   // 文章有分类 先查出分类
   
   $http({
   	  method:'get',
   	  url:'../Api/admin.php?action=article_cate'
   }).then(function(res){
       $scope.article_list=res.data;
   });

 $scope.uploadImg = function(){
        if($scope.food.art_img){
            console.log($scope.food.art_img);
            Upload.upload({
                url:"../Api/admin.php?action=article_add",
                file:$scope.food.art_img
            }).success(function(result){
                $scope.food.art_img = "uploads/"+result.art_img;
                 
                console.log($scope.food.art_img);

            });
        }
    };
    //提交表单  点击添加表单的方法 post提交
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=article_add",
            data:$scope.food,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('article_list');
            }else{
                alert('添加失败');
                $location.path('article_list');
            }
        });
    }

});

// 文章编辑页  用于显示旧信息 以及提交修改的信息

food.controller('ArticleEditeConroller',function($scope,$http,Upload,$location,$stateParams,time){
   
   $http({
      method:'get',
      url:'../Api/admin.php?action=article_detail&art_id='+$stateParams.art_id
   }).then(function(res){
        $scope.article=res.data;
        $scope.article.art_time=time.timestampToTime($scope.article.art_time);
   });

      // 查询文章分类
     $http({
   	  method:'get',
   	  url:'../Api/admin.php?action=article_cate'
   }).then(function(res){
       $scope.article_list=res.data;
   });



   //如果是修改的内容
   
   $scope.uploadImg = function(){
   	// 如果接收到  ng-model="article.art_time"
        if($scope.article.art_img){
            console.log($scope.article.art_img);
            Upload.upload({
                url:'../Api/admin.php?action=article_edite',
                file:$scope.article.art_img
            }).success(function(result){
                $scope.article.art_img = "uploads/"+result.art_img;
                console.log($scope.article.art_img);
            });
        }
    };
    //提交表单  点击添加表单的方法 post提交
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:'../Api/admin.php?action=article_edite&art_id='+$stateParams.art_id,
            data:$scope.article,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('article_list');
            }else{
                alert('修改失败');
                $location.path('article_list');
            }
        });
    }

});

// 删除文章数据的控制器

food.controller('ArticleDeleteConroller',function($scope,$http,$stateParams,$location){
   $http({
    method:'get',
    url:'../Api/admin.php?action=article_delete&art_id='+$stateParams.art_id
   }).then(function(res){

      if(res.data){
             
            $location.path('article_list');

      }else{
            alert('删除失败');
            $location.path('article_list');
      }


   })
  
});

// 文章主类的控制器

food.controller('ArticleCateListConroller',function($http,page,$scope){

   $http({
    method:'get',
    url:'../Api/admin.php?action=art_count'
   }).then(function(res){

      $scope.totalItems=res.data.c;
   });

 // 组装分页
    $scope.currentPage = 1;
    $scope.bigTotalItems = 8;
    $scope.maxSize = 5;

    var url = "../Api/admin.php?action=article_cate_list";
    
    // 默认第一页的页码  数据默认第一页显示
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
        
        $scope.article_cate_list = result;

        console.log($scope.article_cate_list);
        
    });
    
    // 页码改变时 数据根据页码改变而改变
    $scope.pageChanged = function(){
        page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
            $scope.article_cate_list = result;

        });
    };



});

//文章分类控制器

food.controller('ArticleCateAddConroller',function($http,$location,$scope){

      $scope.saveForm = function(){
        $http({
            method:"POST",
            url:"../Api/admin.php?action=article_cate_add",
            data:$scope.art,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){
                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }
                return str.join("&");
            }
        }).then(function(result){
            if(result.data){
                $location.path('article_cate_list');
            }else{
                alert('添加失败');
                $location.path('article_cate_list');
            }
        });
    }


});

// 文章类型编辑页

food.controller('ArticleCateEditeConroller',function($http,$stateParams,$scope,$location){
  
   // 旧信息数据
   $http({
        method:"get",
        url:"../Api/admin.php?action=article_cate_detail&artcate_id="+$stateParams.artcate_id      
    }).then(function(res){
        $scope.art= res.data;
    });



    // 修改的数据
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:'../Api/admin.php?action=article_cate_edite&artcate_id='+$stateParams.artcate_id,
            // 提交的数据
            data:$scope.art,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('article_cate_list');
            }else{
                alert('修改失败');
                $location.path('article_cate_list');
            }
        });
    }





});

food.controller('ArticleCateDeleteConroller',function($http,$scope,$stateParams,$location){
    
    $http({
      
      method:'get',
    
      url:'../Api/admin.php?action=article_cate_delete&artcate_id='+$stateParams.artcate_id,

    }).then(function(res){

         if(res.data){
            $location.path('article_cate_list');
      }else{
            alert('删除失败');
            $location.path('article_cate_list');
      }

    });


});


